//index.js
//获取应用实例
const app = getApp();
//引用腾讯地图API
var QQMapWX = require("../../libs/qqmap-wx-jssdk.js");
var qqmapsdk = new QQMapWX({
    //此key需要用户自己申请
    key: "F4BBZ-M5ARR-VDPWK-WHWLZ-AFX5F-WWFVB",
});

import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    data: {
        homes: true,
        newWx: wx.canIUse("button.open-type.getUserInfo"), //判断微信版本过低区域
        swiperList: [], //轮播数据
        iconIndex: [], //热门推荐数据
        theNewData: [], //新品推荐数据
        shoppingList: [], //商品数据
        active: 0,
        scrollTop: 0, //默认距离顶部为0
        address: "",
        src: "",
        page: 1, //页码
        size: 10, //条数
        text: "", // 公告内容
        headlineLsit: [], //标题数据
        index: "",
        paranoia: false
    },
    totalPages: 1, // 总页数
    //获取二级分类要传的数据
    secondary: {
        page: 1, //页数
        size: 10, //条数
        goodName: "", //商品的名称
        classifyOneId: "", //一级的id
        classifyTwoId: "", //二级分类的id
        salesOrderType: 2, //销量排序 1升序2降序
        priceOrderType: 2, //价格排序 1升序2降序
    },
    // 页面加载时
    onLoad: function(options) {
        // 轮播图的数据
        this.getSwiperList();
        this.affiche();
        //得到用户当前的地址
        let addresOne = wx.getStorageSync("address");
        var that = this;
        // 判断用户有没当前地址
        if (addresOne) {
            this.setData({
                address: addresOne,
            });
        } else {
            // 调用接口
            qqmapsdk.reverseGeocoder({
                success: function(res) {
                    console.log(res);
                    that.setData({
                        address: res.result.address,
                    });
                },
            });
        }
        this.recommendPost(); //热品推荐数据
        this.theNewDataPost(); //新品推荐数据
        this.recommendation();
    },
    //新人优惠券
    async newParanoiaPost() {
        let res = await request({ url: "Coupon/show" });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                paranoia: res.data,
            });
        }
    },
    // 获取轮播图的数据
    async getSwiperList() {
        let res = await request({ url: "Banner/findAll" });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                swiperList: res.data,
            });
        }
    },
    //轮播图按钮
    slideshowBtn(e) {
        console.log(e);
        let url = e.currentTarget.dataset.url;
        if (url == "") {
            return;
        } else {
            wx.navigateTo({
                url: "../out/out?url=" + url,
            });
        }
    },
    //热门推荐数据
    async recommendPost() {
        let res = await request({
            url: "Goods/findNewAll",
            data: {
                page: this.data.page,
                size: this.data.size,
                classifyId: null,
                orderType: 1,
            },
        });
        const total = res.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.data.size);
        console.log(res, "热门数据");
        if (res.code == 200) {
            this.setData({
                iconIndex: [...this.data.iconIndex, ...res.data.records],
            });
        }
    },
    //新品推荐数据
    async theNewDataPost() {
        let res = await request({
            url: "Goods/findNewAll",
            data: {
                page: this.data.page,
                size: this.data.size,
                classifyId: null,
                orderType: 2,
            },
        });
        const total = res.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.data.size);
        console.log(res, "新品数据");
        if (res.code == 200) {
            this.setData({
                theNewData: [...this.data.theNewData, ...res.data.records],
            });
        }
    },
    //热品推荐中的商品按钮
    hotItemBtn(e) {
        console.log(e);
        wx.navigateTo({
            url: "../commodity_details/commodity_details?goodsId=" +
                e.currentTarget.dataset.id,
        });
    },
    //新品推荐标题
    async recommendation() {
        let res = await request({ url: "Classify/findUserAll", data: { pid: 0 } });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                headlineLsit: res.data,
            });
        }
    },
    //新品推荐的标题按钮
    vanTabs(e) {
        console.log(e.detail.index);
        this.setData({
            index: e.detail.index - 1,
            shoppingList: [],
        });
        this.secondary.classifyOneId = e.target.dataset.id;

        this.productData(); //商品数据
    },
    //商品数据
    async productData() {
        let res = await request({
            url: "Goods/findUserAll",
            data: this.secondary,
        });
        console.log(res, "商品数据");
        // 获取 总条数
        const total = res.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.data.size);
        this.setData({
            shoppingList: [...this.data.shoppingList, ...res.data.records],
        });
    },

    //判断第几次加载
    obtainPosition(options) {
        /*判断是第一次加载还是从position页面返回     如果从position页面返回，会传递用户选择的地点*/
        if (options.address != null && options.address != "") {
            //设置变量 address 的值
            this.setData({
                address: options.address,
            });
        }
    },
    //获取公告的内容
    async affiche() {
        let res = await request({ url: "Notice/findByTime" });
        console.log(res);
        if (res.code == 200) {
            this.setData({
                text: res.data.text,
            });
        }
    },
    //轮播图按钮
    imageBtn(e) {
        console.log(e);
        wx.reLaunch({
            url: e.target.dataset.url,
        });
    },
    //新人优惠券按钮
    paranoiaBtn() {
        if (wx.getStorageSync("token")) {
            wx.navigateTo({
                url: '../newParanoia/newParanoia',
            });
        } else {
            wx.showToast({
                title: "请登录！",
                icon: 'none',
                duration: 1500,
            });

        }

    },
    //点击当前地址跳转到切换地址
    onChangeAddress(e) {
        wx.navigateTo({
            url: "../position/position",
        });
    },
    //搜索按钮
    btnInput() {
        wx.navigateTo({
            url: "../../pages/search/search",
        });
    },
    // 公告按钮
    announcement(e) {
        console.log(e);
        wx.navigateTo({
            url: "../announcement/announcement",
        });
    },
    // 时令蔬菜
    seasonalBtn() {
        wx.reLaunch({
            url: "../classify/index?type=0",
        });
    },
    // 鲜活水产
    freshAquaticBtn() {
        wx.reLaunch({
            url: "../classify/index?type=1",
        });
    },
    // 时令水果
    seasonalFruitsBtn() {
        wx.reLaunch({
            url: "../classify/index?type=2",
        });
    },
    //肉禽蛋品
    meatAndBtn() {
        wx.reLaunch({
            url: "../classify/index?type=3",
        });
    },
    // 乳品烘焙
    dairyDakingBtn() {
        wx.reLaunch({
            url: "../classify/index?type=4",
        });
    },
    // 熟食卤味
    cookedFoodBtn() {
        wx.reLaunch({
            url: "../classify/index?type=5",
        });
    },
    // 量贩专区
    volumeZoneBtn() {
        wx.reLaunch({
            url: "../classify/index?type=6",
        });
    },
    // 折扣促销
    discountSales() {
        wx.reLaunch({
            url: "../classify/index?type=7",
        });
    },
    // 粮油调味
    toTasteBtn() {
        wx.reLaunch({
            url: "../classify/index?type=8",
        });
    },
    // 酒水饮料
    winesDrinksBtn() {
        wx.reLaunch({
            url: "../classify/index?type=9",
        });
    },
    //平价蔬菜
    parityVegetables() {
        wx.reLaunch({
            url: "../classify/index?type=10",
        });
    },
    // 野味生活
    gameLife() {
        wx.reLaunch({
            url: "../classify/index?type=11",
        });
    },
    //产地直产
    originDirect() {
        wx.reLaunch({
            url: "../classify/index?type=12",
        });
    },
    onReady: function() {},
    onShow: function() {
        this.newParanoiaPost() //新人优惠券
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    // 滚动条触底事件
    onReachBottom: function() {
        //  1 判断还有没有下一页数据
        if (this.data.page >= this.totalPages) {
            wx.showToast({
                title: "我是有底线的",
                icon: "none",
            });
        } else {
            this.data.page++;
            this.productData();
            this.theNewDataPost();
        }
    },
    onShareAppMessage: function() {},
    // 监听页面滚动事件
    onPageScroll: function(e) {
        // console.log(e);
        this.setData({
            scrollTop: e.scrollTop,
        });
    },
    //item(index,pagePath,text)
    onTabItemTap: function(item) {},
});